package com.ogilvy.cms.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ogilvy.cms.bean.CmsLog;
import com.ogilvy.cms.bean.CmsPage;
import com.ogilvy.cms.bean.CmsPageAudit;
import com.ogilvy.cms.bean.CmsUser;
import com.ogilvy.cms.util.ServiceDispatcher;
import com.ogilvy.common.JSONBean;
import com.ogilvy.common.MetaData;
import com.ogilvy.common.UtilTools;
import com.ogilvy.frame.util.CmsConstants;

@SuppressWarnings("serial")
@Scope("prototype")
@Controller("cms.action.CmsPageAuditAction")
public class CmsPageAuditAction extends AdminAction {
	@Autowired
	protected ServiceDispatcher serviceDispatcher;
	private String rows;//前台以JSON的形式传递数据
	private int start;//分页开始
	private int limit;//每页显示量
	private CmsPageAudit cmspageaudit = new CmsPageAudit();
	List<CmsPageAudit> datalist;
	
	//转到页面审核记录页面中
    public String index()
    {
    	return "pageaudit";
    }
    
	/** 查询 */
	public void query(){
		//设置页面编码
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		//根据查询条件，实现模糊查询
		if(rows==null){
			datalist = serviceDispatcher.getCmsPageAuditService().query(cmspageaudit);
		}else
		{
			net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
			Map<String,String> screenMap = new HashMap<String, String>();
			screenMap.put("cpaCmpId",js.getString("cpaCmpId"));//网页ID
//			screenMap.put("cpaAuditTime",js.getString("cpaAuditTime"));
			screenMap.put("startTime",js.getString("startTime"));
			screenMap.put("endTime",js.getString("endTime"));
			datalist = serviceDispatcher.getCmsPageAuditService().queryByScreen(screenMap);
		}
		
		/*CPA_CMP_ID/cpaCmpId是页面审核表中的字段/成员变量，显示网页ID*/
//		String pageName="";
//		for (int i = 0; i < datalist.size(); i++) {
//			CmsPageAudit cpa = datalist.get(i);
//			pageName = cpa.getCpaCmpId()+"";
//		}
		
		List<CmsPage> datacmspagelist ;
		CmsPage cmsPage = new CmsPage();
		datacmspagelist = serviceDispatcher.getCmsPageService().query(cmsPage);
		for (int i = 0; i < datacmspagelist.size(); i++) {
			CmsPage cmspage01 = datacmspagelist.get(i);
			String pageName = cmspage01.getCmpTitleName();/*获取页面名称*/
			System.out.println(pageName);
			
		}
		
		
		
		
		//datalist = serviceDispatcher.getCmsPageAuditService().query(cmspageaudit);
		//采用集合分页
		List pageList = UtilTools.queryForPage(start, limit, datalist);
		JSONBean jbean = new JSONBean();
		MetaData md = new MetaData();

		md.setIdProperty("cpaId");//关联将要显示列表对应的bean实体的主键ID
		md.reflectFields("com.ogilvy.cms.bean.CmsPageAudit");//关联将要显示列表对应的bean实体
		jbean.setMetaData(md);
		jbean.setRows(pageList);
		jbean.setSuccess(true);
		jbean.setTotal(datalist.size());

		//String jsonString = UtilTools.encodeObject2Json(jbean);
		String jsonString = UtilTools.encodeObject2Json(jbean,"MM/dd/yyyy HH:mm:ss");
		
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//return "query";
	}
	
	/** 新增 */
	public void add(){
		net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
		Integer cpaCmpId = js.getInt("cpaCmpId");			//网页ID
		String cpaType = js.getString("cpaType");			//审核事件
		String cpaComments = js.getString("cpaComments");	//说明
		
		cmspageaudit.setCpaCmpId(cpaCmpId);
		cmspageaudit.setCpaType(cpaType);
		cmspageaudit.setCpaComments(cpaComments);
		//审核时间,获取当前系统时间
		Date getCurrentDate = new Date();
		Date cpaAuditTime = getCurrentDate;	
		cmspageaudit.setCpaAuditTime(cpaAuditTime);
		//审核人就是登陆者，从session中取
		CmsUser cmsuser = (CmsUser)ServletActionContext.getRequest().getSession().getAttribute(CmsConstants.ADMIN_USER);
		String	cpaAudituser = cmsuser.getCmuName();
		cmspageaudit.setCpaAudituser(cpaAudituser);		//获取到session中保存的姓名，即审核人
		
		CmsPageAudit cmsPageAudit = serviceDispatcher.getCmsPageAuditService().add(cmspageaudit);
		JSONBean bean = new JSONBean();
		bean.setSuccess(true);
		// 需要新建一个具有id的vo返回给前台以便通知前台保存成功，不然前台一直处于编辑状态
		List rows = new ArrayList();
		rows.add(cmsPageAudit);
		bean.setRows(rows);
		//String jsonString = UtilTools.encodeObject2Json(bean);
		String jsonString = UtilTools.encodeObject2Json(bean,"MM/dd/yyyy HH:mm:ss");
		
		/**
		 * 系统日志
		 */
		CmsLog  log = UtilTools.buildLog("页面审核", CmsLog.Add, "添加XXXXXXXX", cpaAudituser);
		serviceDispatcher.getCmsLogService().add(log);
		
		
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//return "detail";
	}
	
	/** 修改 */
	public void modify(){
		System.out.println("modify");
		net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
		Integer cpaId = js.getInt("cpaId");
		Integer cpaCmpId = js.getInt("cpaCmpId");
		String cpaType = js.getString("cpaType");
		String cpaComments = js.getString("cpaComments");
		
		
		
		//根据用户选择的数据字典ID，将查询出来。先查询出已经存在的属性，然后对需要修改的属性进行修改
		//cmspageaudit = serviceDispatcher.getCmsPageAuditService().get(cpaId);
		
		cmspageaudit.setCpaId(cpaId);
		cmspageaudit.setCpaCmpId(cpaCmpId);
		cmspageaudit.setCpaType(cpaType);
		cmspageaudit.setCpaComments(cpaComments);
		//修改时，重新获取当前时间
		Date cpaAuditTime = UtilTools.getCurrrentDate1();	
		cmspageaudit.setCpaAuditTime(cpaAuditTime);
		//审核人，重新获取session中的值
		CmsUser cmsuser = (CmsUser) ServletActionContext.getRequest().getSession().getAttribute(CmsConstants.ADMIN_USER);
		String cpaAudituser = cmsuser.getCmuName();
		cmspageaudit.setCpaAudituser(cpaAudituser);

		serviceDispatcher.getCmsPageAuditService().modify(cmspageaudit);
		JSONBean bean = new JSONBean();
		bean.setSuccess(true);
		//String jsonString = UtilTools.encodeObject2Json(bean);
		String jsonString = UtilTools.encodeObject2Json(bean,"MM/dd/yyyy HH:mm:ss");
		
		/**
		 * 系统日志
		 */
		CmsLog cmsLog = UtilTools.buildLog("页面审核", CmsLog.MODIFY, "修改了XXXXXXXX", cpaAudituser);
		serviceDispatcher.getCmsLogService().add(cmsLog);
		
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//return "detail";
	}
	
	public CmsPageAudit getCmspageaudit() {
		return cmspageaudit;
	}

	public void setCmspageaudit(CmsPageAudit cmspageaudit) {
		this.cmspageaudit = cmspageaudit;
	}



	private Integer cpaId;

	public Integer getCpaId() {
		return cpaId;
	}

	public void setCpaId(Integer cpaId) {
		this.cpaId = cpaId;
	}

	public List<CmsPageAudit> getDatalist() {
		return datalist;
	}

	 /** 删除 */
    public void del(){
    	net.sf.json.JSONArray objArr = net.sf.json.JSONArray.fromObject(rows);
        for(int i=0;i<objArr.size();i++)
        {
        	net.sf.json.JSONObject js = objArr.getJSONObject(i);
        	//将 String 转化为 Integer 删除
        	String cpaid = js.getString("cpaId");
        	Integer cpaId = Integer.valueOf(cpaid);
            serviceDispatcher.getCmsPageAuditService().del(cpaId);
        }
        //获取数据
        StringBuffer jsonString =new StringBuffer();
        jsonString.append("{");
        jsonString.append("success:true,");
        jsonString.append("msg:'删除成功'");
        jsonString.append("}");
        /**
         * 系统日志
         */
        CmsUser cmsuser = (CmsUser) ServletActionContext.getRequest().getSession().getAttribute(CmsConstants.ADMIN_USER);
        String cpaAudituser = cmsuser.getCmuName();
        CmsLog cmsLog = UtilTools.buildLog("页面审核", CmsLog.DELETE, "删除XXXXXXX", cpaAudituser);
        serviceDispatcher.getCmsLogService().add(cmsLog);
        
        PrintWriter out;
        try {
        	ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
            out = ServletActionContext.getResponse().getWriter();
            out.print(jsonString.toString());
            out.flush();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	

	
}
